<template>
  <div>
    <basic-container>
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>内容管理</el-breadcrumb-item>
        <el-breadcrumb-item>马甲管理</el-breadcrumb-item>
      </el-breadcrumb>
    </basic-container>
    <basic-container>
      <avue-crud 
        ref="crud"
        :data="data"
        v-model="form"
        :option="option"
        :page.sync="page"
        @on-load="getList"
        @refresh-change="refreshChange"
        @search-reset="form={}"
        @search-change="searchChange"
        @row-update="rowUpdate"
        @row-del="rowDel"
        @row-save="rowSave">
        <template slot="last" slot-scope="scope">
          {{ scope.row.last_req_time }}
          <!-- <el-tag>{{ scope.row.edit_account_name }}</el-tag> -->
        </template> 
        <!-- 自定义按钮 -->
        <template slot-scope="scope" slot="menuLeft">
          <el-button
            type="primary"
            icon="el-icon-plus"
            size="small"
            plain
            @click.stop="handleHeadImage()"
            >头像库</el-button>
           <el-button
            type="primary"
            icon="el-icon-plus"
            size="small"
            plain
            @click.stop="handleAddAll()"
            >批量生成马甲</el-button>
          <!--<el-button
            type="primary"
            icon="el-icon-plus"
            size="small"
            plain
            @click.stop="handleAdd()"
            >马甲账号</el-button> -->
        </template>
         <!-- 自定义操作栏 --> 
        <template slot-scope="scope" slot="menu">
          <!-- <el-button icon="el-icon-edit" type="text" size="small" @click.stop="handleEdit(scope)"
            >编辑</el-button > -->
          <!-- <el-button icon="el-icon-delete" type="text" size="small"  @click.stop="handleDel(scope)"
            >删除</el-button > -->
        </template>
      </avue-crud>
    </basic-container>
    
<el-dialog
  title="批量生成马甲"
  :visible.sync="dialogVisible"
   :modal='false'
  width="30%">
  <span>
    <el-form ref="form" :model="formTemp" label-width="80px"> 
      <el-form-item label="马甲昵称">
        <el-input type="textarea" :rows="10" v-model="formTemp.nickname" placeholder="一行一个昵称"></el-input>
      </el-form-item>
    </el-form>
  </span>
  <span slot="footer" class="dialog-footer">
    <el-button @click="dialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="addNameAll">确 定</el-button>
  </span>
</el-dialog>
  </div>
</template>

<script>
import { common } from "@/api/cat/httpRequest";
import { catUrl } from "@/config/env";
import { fakeUser as baseURL,uploadFile } from "@/api/cat/baseUrls";
export default {
 data() {
    return {
      dialogVisible:false,
      page: {
        total: 0,
        pageSize: 10
      },
      type: true,
      data: [],
      params: {},
      formTemp:{},
      form: {},
      default: {
        orderByList: "reg_time&DESC",
        is_vest:1
      },
      option: {
        addBtn: true,
        editBtn: true,
        viewBtn: false,
        delBtn: true,
        excelBtn: false,
        selection: true,
border: true,
        column: [
          {
            label: "马甲id",
            prop: "user_id",
            type: "number", 
            search: true,
            addDisplay: false,
            editDisplay: false,
            addDisabled: true,
            editDisabled: true, 
          },
          {
            label: "马甲昵称",
            prop: "user_name",
            type: "input", 
            search: true,
          },
          {
            label: "身份",
            prop: "identity",
            type: "select", 
            search: true,
            dicData:[
              {
                label:"普通马甲",
                value:0
              },
              {
                label:"插画师",
                value:2
              },
              {
                label:"作者",
                value:1
              }
            ]
          },
          {
            type: "upload",
            label: "马甲头像",
            span: 24,
            display: true,
            showFileList: true, 
            canvasOption: {},  
            prop: "headimgurl", 
            listType: "picture-img",
            action: `${catUrl}${uploadFile.uploadFile}`,
            propsHttp: {
              url: "source",
            },
            rules: [
              {
                required: true,
                message: "马甲头像必须上传",
              },
            ],
          }, 
          {
            label: "最后操作信息",
            prop: "last",
            type: "input",
            slot:true,
            addDisplay: false,
            editDisplay: false,
            addDisabled: true,
            editDisabled: true,
          },
        ],
      },
    };
  },
  methods: {

    /**
     * 批量新增马甲昵称
     */
    async addNameAll(){
      //逗号和换行的过滤,将内容变为数组格式,全部使用逗号拼接
      let arrs = this.formTemp.nickname.replace(/\n+/g,'\n').split('\n')
      // let arr = this.formTemp.nickname.replace(/,+/g,",").split(/,|\r\n|\n/).filter(e=>/\d/.test(e)).map(e=>e.replace(/\D/g,""));
      // let arrs = Array.from(new Set(arr));
      this.formTemp.user_name = arrs.join(",");
      // let userInfo = this.$store.state.user.userInfo
      // this.formTemp.edit_account_name = userInfo.username;
      // this.formTemp.edit_time = new Date();
      delete this.formTemp.nickname;
      let data = await common(baseURL.addNameAll,this.formTemp,this.page,"POST");
      if(data==null){
        return;
      }

      this.dialogVisible = false;
      this.formTemp = {};
      this.getList();
    },
    /**批量新增马甲昵称弹出 */
    handleAddAll(){
      this.dialogVisible = true;
      this.formTemp = {};
    },
    //跳转至头像库
    handleHeadImage(row){
      this.$router.push("/fakeuser/headimage")
    },
    /**获取列表 */
    async getList() {
      this.loading = true;
      //清空表格列表
      this.data = [];
      //发送请求获取数据
      let response = await common(
        baseURL.pageAndParam,
        {...this.params,...this.default},
        this.page,
        "POST"
      );
      this.page = response.page;
      this.data = response.data;
      this.loading = false;
    },
    /**保存 */
    async rowSave(row, done,loading) {
      delete row["id"];
      // row.create_time = new Date();
      // let userInfo = this.$store.state.user.userInfo
//       scope.row.admin_id = userInfo.id ? userInfo.id : null
//       scope.row.admin_name = userInfo.username
      // row.edit_account_name = userInfo.username;
      // row.edit_time = new Date();
      row.user_name = row.user_name.replace(/\s+/g,'')
      let data = await common(baseURL.save, row, null, "POST");
      if (data==null) {
        loading();
        return;
      }
      this.getList();
      done();
    },
    /**修改 */
    async rowUpdate(row, index, done, loading) {
      // let userInfo = this.$store.state.user.userInfo
      // row.edit_account_name = userInfo.username;
      // row.edit_time = new Date();
      let data = await common(baseURL.update, row, null, "POST");
      if (data==null) {
        loading();
        return;
      }
      this.getList();
      done();
    },
    /**刷新 */
    refreshChange() {
      this.getList();
    },
    /**删除 */
    rowDel(row) {
      this.$confirm("此操作将永久删除, 是否继续?", "提示", {
        type: "warning",
      })
        .then(async () => {
          let response = await common(
            baseURL.delete,
            { id: row.user_id },
            null,
            "GET"
          );
          // if (response == null) {
          //   return this.$message.error("操作失败");
          // }
        })
        .then(() => {
          this.getList();
        });
    },
    /**搜索 */
    searchChange(params, done) {
      
      this.params = params;
      this.page.currentPage = 1;
      this.getList();
      if (done) done();
    },
  },
}
</script>

<style>

</style>